<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
model('core/core')->checklogin('jy_mbaoming/index/');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$activityid=intval($_RQ['activityid']);
	if(!$activityid){
		exi('请选择活动','error');
	}
	$activity=pdo_get('jy_baoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$activityid));
	$activity['diyfrom']=$activity['diyfrom']?json_decode($activity['diyfrom'],true):array();
	if(!$activity['diyfrom']){
		$activity['diyfrom']=array('type'=>1,'title'=>'联系人','data'=>'');
	}
	$return['adddiydata']=array(
		'key'=>0,
		'diyfields'=>$activity['diyfrom'],
	);
	$members[]=$return['adddiydata'];
	$post=array(
		'diyfields'=>$activity['diyfrom'],
		'activity_id'=>$activityid,
		'money'=>$activity['money'],
		'members'=>$members
	);
	$return['activity']=$activity;
	$return['xieyi']=$this->plugin_setting('xieyi'.$_SESSION['onemerchid']);
	$endtime=strtotime($activity['end']);
	if($endtime<time()){
		exi('活动已截至！不能再报名！','error','goback');
	}
	$total=pdo_getsum('jy_baoming_order',array('status'=>array(1,2,4),'uniacid'=>$_SESSION['uniacid'],'activity_id'=>$activity['id']),'total');
	if($activity['mannum']<=$total){
		exi('报名人数已满！不能再报名！','error','goback');
	}
	$return['post']=$post;
	exi($return);
}
//支付
if($_RQ['op']=='notice'){
	$post=get_postdata();
	if(empty($post['checked'])){
		exi('请阅读报名注意事项','error');
	}
	$activity=pdo_get('jy_baoming_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['activity_id']));
	$endtime=strtotime($activity['end']);
	if($endtime<time()){
		exi('活动已截至！不能再报名！','error');
	}
	$total=pdo_getsum('jy_baoming_order',array('status'=>array(1,2,4),'uniacid'=>$_SESSION['uniacid'],'activity_id'=>$post['activity_id']),'total');
	if($activity['mannum']<=$total){
		exi('报名人数已满！不能再报名！','error');
	}
	//判断是否有数据
	if(empty($post['name'])){
			//exi('请填写姓名','error');
	}
	if(empty($post['mobile'])){
			//exi('请填写手机号码','error');
	}
	if(!preg_match("/^1[345678]{1}\d{9}$/",$post['mobile'])){  	 
			//exi('请输入正确的手机号码','error');
	}
	foreach($post['members'] as $mv) {
		foreach($mv['diyfields'] as $item){
			if($item['ismust']==1 && empty($item['data'])){
				exi('请完善'.$item['title'].'内容后再提交','error');
			}
		}
	}
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'user_id'=>$_SESSION['uid'],
			'activity_id'=>$post['activity_id'],
			'money'=>$post['money'],
			'pay'=>$post['pay'],
			'name'=>$post['name'],
			'mobile'=>$post['mobile'],
			'remark'=>trim($post['remark']),
			'members'=>ijson_encode($post['members']),
			'total'=>count($post['members']),
			'ordersn'=>get_ordersn('jy_baoming_order','ordersn'),
			'createtime'=>TIMESTAMP,
	);
	$insert['merchid']=$_SESSION['onemerchid'];
	pdo_insert('jy_baoming_order',$insert);
	$id=pdo_insertid();
	if($post['money']>0){
			$params['fee']=$post['money'];
			$params['title']='活动报名订单';
			$params['id']=$id;
			$params['plugin']='jy_mbaoming';
			$params['back_url']='jy_mbaoming/book/payresult/';
			$payid=cfc('pay')->create_pay_order($params);
			pdo_update('jy_baoming_order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
					'pay_id'=>$payid,
					'order_id'=>$id,
			);
			exi($res);
	}else{
			pdo_update('jy_baoming_order',array('status'=>'2'),array('id'=>$id));
			exi('报名成功','success','jy_mbaoming/order/list/status=2&id='.$id);
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_baoming_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_baoming_order',array('status'=>'2','ispay'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	exi('支付成功','success','jy_mbaoming/order/list/status=2&id='.$order['id']);
}